package com.jicheng.controller;

import com.jicheng.Utils.Base64Utils;
import com.jicheng.Utils.DESEncryptUtil;
import io.swagger.annotations.Api;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.UUID;

/**
 * @ClassName: AccountController
 * @Description: TODO
 * @author: khz
 * @date: 2020年08月28日 10:56
 */
@Controller
@RequestMapping(value = "/account")
public class AccountController {

    @ResponseBody
    @RequestMapping(value = "/multipartFileToFile", method = RequestMethod.POST)
    public static File multipartFileToFile(MultipartFile multipartFile) {
        String tempFilePath = "H:\\zz";
        File file = new File(tempFilePath);
        // 获取文件原名
        String originalFilename = multipartFile.getOriginalFilename();
        // 获取文件后缀
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        if (!file.exists()) {
            file.mkdirs();
        }
        // 创建临时文件
        File tempFile = new File(tempFilePath + "\\" + UUID.randomUUID().toString().replaceAll("-", "") + suffix);
        try {
            if (!tempFile.exists()) {
                // 写入临时文件
                multipartFile.transferTo(tempFile);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return tempFile;
    }

    @ResponseBody
    @RequestMapping(value = "/getByte", method = RequestMethod.GET)
    public byte[] getByte(String path) {
        try {
            byte[] bytes = DESEncryptUtil.decryption2Byte("H:/key.k", path, "decrypt");
//            byte[] bytes = Base64Utils.fileToByte(path);
            return bytes;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }



}
